<?php

namespace app\models;

use Yii;
use yii\helpers\Html;

/**
 * This is the model class for table "comment".
 *
 * @property integer $id
 * @property string $commenter
 * @property string $email
 * @property integer $pcid
 * @property string $pubtime
 * @property string $content
 * @property integer $aid
 * @property integer $subscribe
 *
 * @property Article $a
 */
class Comment extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'comment';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['commenter', 'email', 'aid', 'pcid'], 'required'],
            ['commenter', 'isMe'],
            ['email', 'email'],
            [['pcid', 'aid', 'subscribe'], 'integer'],
            [['pubtime'], 'safe'],
            [['content'], 'string'],
            [['commenter', 'email'], 'string', 'max' => 255]
        ];
    }

    /**
     * isMe
     * 不能是我自己, 纯粹的演示作用
     *
     * @param mixed $attr
     * @param mixed $para
     * @access public
     * @return void
     */
    public function isMe($attr, $para)
    {
        if ($this->$attr == 'sidgwick') {
            $this->addError($attr, "Shit, the name can't be sidgwick");
            return false;
        }
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'commenter' => 'Commenter',
            'email' => 'Email',
            'pcid' => 'Pcid',
            'pubtime' => 'Pubtime',
            'content' => 'Content',
            'aid' => 'Aid',
            'subscribe' => 'Subscribe',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getArticle()
    {
        return $this->hasOne(Article::className(), ['id' => 'aid']);
    }

    /**
     * getServerValidateErrors
     * 生成AJAX提交时的错误提示信息
     *
     * @access public
     * @return void
     */
    public function getServerValidateErrors()
    {
        $errmsg = [];
        foreach ($this->getErrors() as $attribute => $errors) {
            $errmsg[Html::getInputId($this, $attribute)] = $errors;
        }
    }
}
